package com.hhkj.admin.course.common.component.supplier;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.admin.course.domain.Course;
import com.hhkj.admin.course.domain.CourseBankType;
import com.hhkj.admin.course.service.base.CourseBankTypeService;
import com.hhkj.admin.course.domain.CoursePriceConf;
import com.hhkj.admin.course.service.base.CoursePriceConfService;
import com.ruoyi.common.cont.SysConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * CourseSupplier
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/13 15:36
 */
@Component
public class CourseSupplier
{

    @Autowired
    private CourseBankTypeService bankTypeService;

    @Autowired
    private CoursePriceConfService coursePriceConfService;

    /**
     * 给对应课程设置题库类型列表
     *
     * @param coursePoAllList 课程信息列表
     */
    public void supplyBankTypeList(List<Course> coursePoAllList)
    {
        if (CollectionUtil.isEmpty(coursePoAllList)) {
            return;
        }
        List<Long> courseIdList
                = coursePoAllList.stream().map(Course::getId).collect(Collectors.toList());

        List<CourseBankType> bankTypeList = bankTypeService.list(
                new LambdaQueryWrapper<CourseBankType>()
                        .in(CourseBankType::getCourseId, courseIdList)
                        .eq(CourseBankType::getDisableFlag, SysConst.Flag.NO.getFlag()));

        Map<Long, List<CourseBankType>> bankTypeListGroupByCourseId
                = bankTypeList.stream().collect(Collectors.groupingBy(CourseBankType::getCourseId));

        for (Course coursePo : coursePoAllList) {
            if (bankTypeListGroupByCourseId.containsKey(coursePo.getId())) {
                coursePo.setBankTypeList(bankTypeListGroupByCourseId.get(coursePo.getId()));
            }
        }
    }

    /**
     * 给对应课程设置课程价格
     *
     * @param coursePoAllList 课程信息列表
     */
    public void supplyCoursePrice(List<Course> coursePoAllList)
    {
        if (CollectionUtil.isEmpty(coursePoAllList)) {
            return;
        }
        List<Long> courseIdList
                = coursePoAllList.stream().map(Course::getId).collect(Collectors.toList());

        Map<Long, List<CoursePriceConf>> coursePriceListMap = coursePriceConfService.groupByCourseIdList(courseIdList);
        for (Course coursePo : coursePoAllList) {
            if (coursePriceListMap.containsKey(coursePo.getId())) {
                coursePo.setCoursePriceConfList(coursePriceListMap.get(coursePo.getId()));
            }
        }
    }
}
